package fr.ants.controller.command;

import java.io.File;

import fr.ants.controller.Simulator;
import fr.ants.model.Grid;
import fr.ants.model.PictureReader;
import fr.ants.view.panel.Canvas;

/**
 * <p>
 * Command to load the map.
 * </p>
 * 
 * @author Thierno BALDE
 */

public class LoadImageCommand implements Command {
	/**
	 * the picture.
	 */
	private final File file;
	/**
	 * Simulator.
	 */
	private final Simulator simulator;
	/**
	 * Canvas.
	 */
	private final Canvas canvas;
	/**
	 * To read the picture.
	 */
	private final PictureReader pictureReader = new PictureReader();

	/**
	 * Build.
	 * 
	 * @param file
	 *            file
	 * @param simulator
	 *            simulator
	 * @param canvas
	 *            cancas
	 */
	public LoadImageCommand(final File file, final Simulator simulator,
			final Canvas canvas) {
		this.file = file;
		this.simulator = simulator;
		this.canvas = canvas;
	}

	@Override
	public void execute() {
		int[][] gridData = pictureReader.loadGrid(file);
		Grid grid = new Grid(gridData[0].length, gridData.length, gridData);
		simulator.setGrid(grid);
		canvas.update(grid);
		canvas.repaint();
	}

}
